package academia.persistencia;

import academia.pessoal.Cliente;
import academia.pessoal.Instrutor;
import academia.pessoal.Pessoa;
import academia.pessoal.Treino;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.TableModel;
import net.proteanit.sql.DbUtils;

public class TreinoDAO {

    private boolean resp;

    public boolean criarFicha(int idInstrutor, int idCliente, String textotreino) {

        Connection conn = null;
        PreparedStatement ps = null;

        conn = DBAcademia.ConnectDB();
        try {
            PessoaDAO dao = new PessoaDAO();
            Treino treino = new Treino();
            String sql = "INSERT INTO Treino (Idaluno,Idinstrutor,Aluno,Instrutor,Data,Treino) VALUES(?,?,?,?,?,?)";

            ps = conn.prepareStatement(sql);
            
            ps.setInt(1, idCliente);
            ps.setInt(2, idInstrutor);
            ps.setString(3, dao.buscar(idCliente));
            ps.setString(4, dao.buscar(idInstrutor));
            ps.setInt(5, Calendar.DAY_OF_MONTH);
            ps.setString(6, textotreino);
            ps.executeUpdate();

            resp = true;
        } catch (SQLException ex) {
            ex.printStackTrace();
            resp = false;

        } finally {
            close(ps, conn, null);
        }
        return resp;
    }

    private void close(PreparedStatement ps, Connection conn, ResultSet rs) {
        RuntimeException r = null;
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException ex) {
                r = new RuntimeException(ex);
                Logger.getLogger(PessoaDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException ex) {
                r = new RuntimeException(ex);
                Logger.getLogger(PessoaDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException ex) {
                r = new RuntimeException(ex);
                Logger.getLogger(PessoaDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (r != null) {
            throw r;
        }
    }

    public TableModel listar(int idaluno) {
        TableModel result = null;
        Connection conn = null;
        ResultSet rs = null;
        PreparedStatement ps = null;
        conn = DBAcademia.ConnectDB();

        try {
            String sql = "SELECT * FROM Treino WHERE IDaluno='" + idaluno + "'";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            result = DbUtils.resultSetToTableModel(rs);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            close(ps, conn, rs);
        }
        return result;
    }
}
